package ldk.util.adapterjoinner.sample;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ldk.util.adapterjoinner.AdapterJoiner;
import ldk.util.adapterjoinner.JoinableAdapter;
import ldk.util.adapterjoinner.R;

/**
 * Created by ldkxingzhe@163.com on 2017/6/6.
 */
public class GridLayoutManagerActivity extends Activity{

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    private GridLayoutManager manager;
    private AdapterJoiner mAdapterJoiner;

    List<String> mNameList = Arrays.asList("name", "first", "seond", "third", "fourth", "fifth");
    List<Integer> mAgeList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
    TestAdapter mFirstAdapter, mSecondAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_layout);
        ButterKnife.bind(this);
        mAdapterJoiner = new AdapterJoiner();
        manager = new GridLayoutManager(this, 2);
        manager.setSpanSizeLookup(mSpanSizeLookup);
        mRecyclerView.setLayoutManager(manager);
        composeView();
        mRecyclerView.setAdapter(mAdapterJoiner.getAdapter());
    }

    private void composeView() {
        mFirstAdapter = new TestAdapter(mNameList.size());
        mSecondAdapter = new TestAdapter(mAgeList.size());
        mAdapterJoiner.add(new JoinableAdapter(mFirstAdapter));
        mAdapterJoiner.add(new JoinableAdapter(mSecondAdapter));
    }

    private GridLayoutManager.SpanSizeLookup mSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (position >= mNameList.size()){
                return 1;
            }
            return 2;
        }
    };
}
